// common/request.js
const baseUrl = 'http://127.0.0.1:7088'; // 替换成你的API接口地址
// const baseUrl = 'http://shop.h5.getgeo.cn'; // 替换成你的API接口地址
import {encrypt,decrypt} from '@/sm4.js';
import {getSign} from '@/sign.js';
export default {
	get(url, data = {}) {
		return new Promise((resolve, reject) => {
			console.log(baseUrl + url)
			uni.request({
				url: baseUrl + url,
				data,
				method: 'GET',
				header: {
					'Content-Type': 'application/json',

				},
				withCredentials: true, // 添加这个配置
				success: (res) => {
					resolve(res.data);
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	},

	post(url, data = {}) {
		return new Promise((resolve, reject) => {
			console.log(baseUrl + url)
			let token=uni.getStorageSync('token')
			
			let sign=getSign(JSON.stringify(data));
			console.log("sign：",sign)
			console.log("data：",data)
			uni.request({
				url: baseUrl + url,
				data:{data:encrypt(JSON.stringify(data))},
				method: 'POST',
				header: {
					'Content-Type': 'application/json', // 根据需要设置请求头
					'token': token,
					'sign':sign,
				},
				withCredentials: true, // 添加这个配置
				success: (res) => {
					resolve(res.data);
					if(res.data.code==-1 && res.data.msg=='token校验失败！'){
						uni.navigateTo({
							url: '/pages/login'
						});
					}
					

				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	},

	// 可以继续定义其他请求方法，如 put、delete 等
};